<template>
  <div>
     <!-- 面包屑导航区 -->
     <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">Music</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getUser"
           
          >
            <el-button
            @click="getUser"
              slot="append"
              icon="el-icon-search"
              
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addusers">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table border 
    :data="userlist"
    stripe
    style="width: 100%">
    <el-table-column
     
    type="index"
      width="80">
    </el-table-column>
    <el-table-column
      prop="username"
      label="姓名"
      width="180">
    </el-table-column>
    <el-table-column
      prop="email"
      label="邮箱"
      width="180">
    </el-table-column>
    <el-table-column
      prop="mobile"
      label="电话">
    </el-table-column>
    <el-table-column
      prop="role_name"
      label="角色">
    </el-table-column>
    <el-table-column
      prop="mg_state"
      label="状态">
      <template #default="{row}">
        
        <el-switch v-model="row.mg_state" @change="update(row)"></el-switch>
      </template>
    </el-table-column>
    <el-table-column
      
      label="操作">
      <template #default="{row}">
        
        <el-button type="primary" icon="el-icon-edit" @click="show(row.id)"></el-button>
        <el-button type="danger" icon="el-icon-delete" @click="removeUserById(row.id)"></el-button>
       <el-button type="warning" icon="el-icon-setting" @click="setRole(row)"></el-button>
      </template>
    </el-table-column>
   
  </el-table>
   <!-- 分页 -->
   <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1,2,5,10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 对话框 -->
    <el-dialog @close="closerest"
  title="添加用户"
  :visible.sync="adduser"
  width="50%"
  >
 <!-- 表单区域 -->
 <el-form :model="addForm" :rules="addFormRules" ref="editFormRef" label-width="70px" >
  <el-form-item label="用户名" prop="username">
    <el-input v-model="addForm.username"></el-input>
  </el-form-item>
   <el-form-item label="邮箱" prop="email">
    <el-input v-model="addForm.email" ></el-input>
  </el-form-item>
   <el-form-item label="手机号" prop="mobile">
    <el-input v-model="addForm.mobile" ></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input v-model="addForm.password" ></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="adduser=false">取 消</el-button>
    <el-button type="primary" @click="addclck">确 定</el-button>
  </span>
</el-dialog>

  <!-- 修改用户对话框 -->
  <el-dialog
  title="修改用户"
  :visible.sync="EditDialogVisible"
  width="50%" 
  >
  <el-form :model="editForm"  ref="editFormRef" label-width="70px" >
  <el-form-item label="用户名" prop="name">
    <el-input v-model="editForm.username" disabled></el-input>
  </el-form-item>
   <el-form-item label="邮箱" prop="email">
    <el-input v-model="editForm.email" ></el-input>
  </el-form-item>
   <el-form-item label="手机号" prop="mobile">
    <el-input v-model="editForm.mobile" ></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="EditDialogVisible =false">取 消</el-button>
    <el-button type="primary" @click="userput">确 定</el-button>
  </span>
</el-dialog>
<!-- 分配角色 -->
<el-dialog
  title="分配角色"
  :visible.sync="fen"
  width="30%"
  >
  <div>
    <p class="c">当前的用户: &nbsp; &nbsp;{{userInfo.username}}</p>
    <p class="b">当前的角色: &nbsp; &nbsp;{{userInfo.role_name}}</p>
    <p class="d">分配新角色:&nbsp; 
      <el-select  placeholder="请选择" v-model="selectedRoleId">
    <el-option 
      v-for="item in rolesList"
      :key="item.id"
      :label="item.roleName"
      :value="item.id">
    </el-option>
  </el-select>
    </p>
  </div>
  <span slot="footer" class="dialog-footer">
    <el-button @click="fen = false">取 消</el-button>
    <el-button type="primary" @click="saveinfo">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { getUserList, updateUser ,addUser,delUser,editUser,editUserList,getJJ,upjj} from '@/api/user'
import {Message} from 'element-ui'
export default {
  created() {
    this.getUser()
  },
  methods: {
    async saveinfo() {
    if(!this.selectedRoleId) return Message.error('请选择角色')
      let res = await upjj(this.userInfo.id, { rid: this.selectedRoleId })
      console.log(res);
      if (res.meta.status !== 200) return Message.error(res.meta.msg)
      Message.success('更新角色成功')
      this.getUser()

      this.fen = false
    },
  async  setRole(userInfo) {
      this.fen = true
      this.userInfo = userInfo
      let res = await getJJ()
      this.rolesList = res.data 
      console.log(this.selectedRoleId);
      
      
    },
    saveRoleInfo(){},
    async userput() {
     let res  = await editUserList(this.editForm.id,this.editForm)
      if (res.meta.status == 200) {
        Message.success('更新成功')
      this.getUser()
      this.EditDialogVisible = false
      } else {
        Message.success('更新失败')
     }
     console.log(res);
      
    },
   async show(id) {
     
      this.EditDialogVisible = true
     let res =  await editUser(id)
      console.log(res);
     this.editForm =res.data

    },
    async removeUserById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        delUser(id)
           this.getUser()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    
   },
   async addclck() {
      await this.$refs.editFormRef.validate()
   
     let res =  await addUser(this.addForm)
      console.log(res);
      if (res.meta.status == 201) {
        Message.success('添加成功')
        this.adduser = false
        
        this.getUser()
       
   
      } else {
        Message.success('添加失败')
     }
     
    },
    closerest() {
      console.log(1);
      
    },
    addusers() {
      this.adduser = true
    },
   async update(val) {
      
      let res = await updateUser(val)
      console.log(res);
      if (res.meta.status !== 200) {
        return Message.error('更新失败')  
      }
      Message.success('更新成功')
       
    
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getUser()
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getUser()
    },
    async getUser() {
      let res = await getUserList(this.queryInfo)
      console.log(res);
      this.userlist = res.data.users
      this.total = res.data.total
    }
  },
  data() {
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }
    return {
      selectedRoleId:'',
      rolesList:[],
      fen:false,
      userInfo:'',
      EditDialogVisible:false,
    editForm: {
        email: '',
        mobile: ''
    },
    addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 添加表单验证规则对象
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }, { min: 3, max: 10, message: '长度在三到十', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 15, message: '长度在6到15', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }, { min: 6, max: 15, message: '长度在6到15', trigger: 'blur' }, ],
        mobile: [{ required: true, message: '请输入手机', trigger: 'blur' }, { min: 6, max: 20, message: '长度在6到15', trigger: 'blur' },{ validator: checkMobile, trigger: 'blur' }]
      },
    adduser:false,
    queryInfo: {
      query: '',
      pagenum: 1,
      pagesize:2
    },
    userlist: [],
    total:0
  }
},
}
</script>

<style scoped>

.c,.b,.d {
  margin-top: 15px;

}
</style>